/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.gui.views;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.toedo.controllers.Controller;
import org.toedo.controllers.LogController;
import org.toedo.gui.translations.Messages;
import org.toedo.logging.Log;
import org.toedo.Activator;

public class DebugView extends ViewPart implements PropertyChangeListener{
	
	public static final String ID = "toedo-core.DebugView"; //$NON-NLS-1$
	
	private Text logText;
	
	private Action forceGCAction;
	private Action copyToClipboardAction;

	public DebugView() {
		LogController.getInstance().addLogChangeListener(this);
	}

	@Override
	public void createPartControl(Composite parent) {
		this.setPartName(Messages.DebugView_Title);
		
		buildActions();
		
		logText = new Text(parent, SWT.READ_ONLY | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		
		doFullUpdate();
	}
	
	private void buildActions() {
		forceGCAction = new Action(Messages.DebugView_ForceGC) {
			public void run() {
				Log.getInstance(DebugView.class).debug("GC forced."); //$NON-NLS-1$
				System.gc();
			}
		};
		forceGCAction.setImageDescriptor(Activator.getImageDescriptor("/icons/forcegc-16.png")); //$NON-NLS-1$
		
		copyToClipboardAction = new Action(Messages.DebugView_CopyToClipboard) {
			public void run() {				
				LogController.getInstance().copyLogToClipboard(); 
			}
		};
		copyToClipboardAction.setImageDescriptor(Activator.getImageDescriptor("/icons/copylog-16.png")); //$NON-NLS-1$
		
		// Toolbar		
		IToolBarManager rootMenuManager = getViewSite().getActionBars().getToolBarManager();
		rootMenuManager.removeAll();
		
		rootMenuManager.add(forceGCAction);
		rootMenuManager.add(copyToClipboardAction);
	}
	
	private void doFullUpdate() {
		if (!Controller.getInstance().isAplicationStopping()) {
			String text = LogController.getInstance().getMessages();
			logText.setText(text);	
			logText.setSelection(text.length());		
		}
	}
	
	private void update(String newLine) {
		if (!Controller.getInstance().isAplicationStopping()) {
			logText.append(newLine);
			logText.setSelection(logText.getText().length());		
		}
	}

	@Override
	public void setFocus() {
		logText.setFocus();
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {		
		if (evt.getPropertyName().equals(LogController.LOG_UPDATE)) {
			class updateLog implements Runnable {

				private String _message;

				public updateLog(String message) {
					_message = message;
				}

				public void run() {
					update(_message);
				}
			}
			Display.getDefault().asyncExec(new updateLog((String) evt.getNewValue()));
		}
	}
	
	public void dispose() {
		LogController.getInstance().removeLogChangeListener(this);
		super.dispose();
	}	
}
